<script setup>
import PageContainer from '@/views/PageContainer.vue'
import { ref, onMounted } from 'vue'
import { useArticleDeleteServices, useArticleListServices } from '@/api/article'
import { dateFormat } from '../../utils/dateFormate.js'
import { Delete, Edit, InfoFilled } from '@element-plus/icons-vue'
import CateSelect from '@/views/cateSelect.vue'
import ArticleEditDrawer from '@/views/article/ArticleEditDrawer.vue'
import { ElMessage } from 'element-plus'
const articleList = ref([])
const total = ref(0)
const selectValue = ref({
  categoryId: '',
  state: null,
  pageNum: 1,
  pageSize: 2
})
const getArticleList = async () => {
  let res = await useArticleListServices(selectValue.value)
  total.value = res.data.total
  articleList.value = res.data.items
}
onMounted(() => {
  getArticleList()
})
const updateCate = (value) => {
  selectValue.value.categoryId = value
}
const articleEditDrawer = ref()
const onEdit = (row) => {
  articleEditDrawer.value.open(row)
}
const handleSizeChange = (val) => {
  selectValue.value.pageSize = val
  selectValue.value.pageNum = 1
  getArticleList()
}
const handleCurrentChange = (val) => {
  selectValue.value.pageNum = val
  getArticleList()
}
const confirmEvent = async (e) => {
  await useArticleDeleteServices(e.id)
  await getArticleList()
  ElMessage.success('删除成功')
}
const cancelEvent = () => {
  console.log('cancel!')
}
</script>

<template>
  <PageContainer title="文章管理">
    <template #header>
      <el-button type="primary" @click="articleEditDrawer.open({})">新增文章</el-button>
    </template>
    <el-form :model="selectValue" label-width="auto" style="max-width: 100%" inline>
      <el-form-item label="文章分类">
        <cateSelect
          v-model:modelValue="selectValue.categoryId"
          @update:modelValue="updateCate"
          width="160px"
        />
      </el-form-item>
      <el-form-item label="发布状态:" style="width: 160px">
        <el-select v-model="selectValue.state" clearable style="width: 240px">
          <el-option label="草稿" value="草稿" />
          <el-option label="已发布" value="已发布" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getArticleList">查询</el-button>
        <el-button type="info">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="articleList" style="width: 100%">
      <el-table-column label="序号" type="index" width="80" align="center"></el-table-column>
      <el-table-column label="文章标题" prop="title" width="380"></el-table-column>
      <el-table-column label="文章分类" prop="categoryName"> </el-table-column>
      <el-table-column label="发布状态" prop="state"> </el-table-column>
      <el-table-column label="发布时间" prop="createTime">
        <template #default="scope">
          {{ dateFormat(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" prop="updateTime">
        <template #default="scope">
          {{ dateFormat(scope.row.updateTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" :icon="Edit" circle @click="onEdit(scope.row)"></el-button>
          <el-popconfirm
            confirm-button-text="Yes"
            cancel-button-text="No"
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="Are you sure to delete this?"
            @confirm="confirmEvent(scope.row)"
            @cancel="cancelEvent"
          >
            <template #reference>
              <el-button type="danger" :icon="Delete" circle></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="selectValue.pageNum"
      v-model:page-size="selectValue.pageSize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <ArticleEditDrawer title="文章编辑" ref="articleEditDrawer" @updateArticle="getArticleList" />
  </PageContainer>
</template>

<style scoped></style>
